package com.langshixiaobai.game;

import com.langshixiaobai.eneity.User;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.WebSocketSession;


import java.util.concurrent.ConcurrentHashMap;

@Component
public class OnlineUserMap {

    //游戏大厅在线状态
    //ConcurrentHashMap 避免线程安全问题 CAS + synchronizes
    private ConcurrentHashMap<Integer, WebSocketSession> gameHall = new ConcurrentHashMap<>();
    //用于存储进入匹配房间的userId
    private ConcurrentHashMap<Integer, WebSocketSession> gameRoom = new ConcurrentHashMap<>();

    public void enterGameHall(Integer userId, WebSocketSession webSocketSession) {
        gameHall.put(userId, webSocketSession);
    }

    public void removeGameHall(Integer userId) {
        gameHall.remove(userId);
    }

    public WebSocketSession getFromGameHall(Integer userId) {
        return gameHall.get(userId);
    }

    public void enterGameRoom(Integer userId, WebSocketSession webSocketSession) {
        gameRoom.put(userId, webSocketSession);
    }

    public void removeGameRoom(Integer userId) {
        gameRoom.remove(userId);
    }

    public WebSocketSession getFromGameRoom(Integer userId){
        return gameRoom.get(userId);
    }

}
